package com.ys.ffms.controller;

import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ys.ffms.common.Result;
import com.ys.ffms.pojo.Bill;
import com.ys.ffms.pojo.DO.BillDO;
import com.ys.ffms.pojo.PO.ExpensePO;
import com.ys.ffms.pojo.PO.IncomePO;
import com.ys.ffms.pojo.VO.queryBillVO;
import com.ys.ffms.service.BillService;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/bill")
public class BillController {

    @Autowired
    private BillService billService;

    public List<queryBillVO> findAll(){
        List<queryBillVO> list = billService.findAll();
        return list;
    }

    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @param details
     * @return
     */
//    @GetMapping("/findAllByPage")
//    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
//                              @RequestParam(defaultValue = "10") Integer pageSize,
//                              @RequestParam(defaultValue = "") String details,
//                              @RequestParam(defaultValue = "") String titleKey) {
//        LambdaQueryWrapper<Bill> wrapper = Wrappers.<Bill>lambdaQuery();
//        //当search为空的时候不进行查询
//        if(StrUtil.isNotBlank(details)){
//            wrapper.like(Bill::getDetails,details);
//        }
//        if(StrUtil.isNotBlank(titleKey)){
//            wrapper.like(Bill::getTitle,titleKey);
//        }
//        Page<Bill> page = new Page<>(pageNum, pageSize);
//        Page<Bill> pageResult = billMapper.selectPage(page, wrapper);
//        return Result.success(pageResult);
//    }


    /**
     * 多表多条件分页查询 收入
     * @param pageNum
     * @param pageSize
     * @param details
     * @param titleKey
     * @return
     */
    @GetMapping("/findAllByPage")
    public Result<Page<queryBillVO>> findAllBill(@RequestParam(defaultValue = "1") Integer pageNum,
                                                 @RequestParam(defaultValue = "10") Integer pageSize,
                                                 @RequestParam(defaultValue = "") String details,
                                                 @RequestParam(defaultValue = "") String titleKey){
        Page<queryBillVO> page = billService.findAllBill(pageNum,pageSize,details,titleKey);

        return Result.success(page);
    }
    /**
     * 多表多条件分页查询 支出
     * @param pageNum
     * @param pageSize
     * @param details
     * @param titleKey
     * @return
     */
    @GetMapping("/findAllByPage2")
    public Result<Page<queryBillVO>> findAllBillExpenditure(@RequestParam(defaultValue = "1") Integer pageNum,
                                                 @RequestParam(defaultValue = "10") Integer pageSize,
                                                 @RequestParam(defaultValue = "") String details,
                                                 @RequestParam(defaultValue = "") String titleKey){
        Page<queryBillVO> page = billService.findAllBillExpenditure(pageNum,pageSize,details,titleKey);

        return Result.success(page);
    }

    /**
     * 添加支出流水 收入
     * @param billDO
     * @return
     */
    @PostMapping("/addWater")
    public Result<?> addWater(@RequestBody BillDO billDO){
        if(billDO!=null){
            billService.addWater(billDO);
            return Result.success(billDO,"添加成功!");
        }else {
            return Result.error("400", "请输入必要参数!");
        }
    }
    /**
     * 添加支出流水 支出
     * @param billDO
     * @return
     */
    @PostMapping("/addWaterExpenditure")
    public Result<?> addWaterExpenditure(@RequestBody BillDO billDO){
        if(billDO!=null){
            billService.addWaterExpenditure(billDO);
            return Result.success(billDO,"添加成功!");
        }else {
            return Result.error("400", "请输入必要参数!");
        }
    }

    /**
     * 按照id删除流水信息 绝对删除
     * @param id
     * @return
     */
    @GetMapping("/delWater/{id}")
    public Result delWater(@PathVariable Integer id){
        billService.delWater(id);
        return Result.success("删除成功!");
    }
    /**
     * 按照id删除流水信息 相对删除
     * @param id
     * @return
     */
    @GetMapping("/delWaterById/{id}")
    public Result delWaterById(@PathVariable Integer id){
        billService.delWaterById(id);
        return Result.success("删除成功!");
    }

    /**
     * 按照id查询一条流水信息
     * @param id
     * @return
     */
    @GetMapping("/CheckOneById/{id}")
    public Result<Bill> CheckOneById(@PathVariable Integer id){
        if(id!=null){
            queryBillVO queryBillVO = billService.CheckOneById(id);

            return Result.success(queryBillVO);
        }else {
            return Result.error("500","ID不能为空");
        }
    }

    /**
     * 收入管理 修改数据
     * @param queryBillVO
     * @return
     */
    @PostMapping("/editWater")
    public Result<?> editWater(@RequestBody queryBillVO queryBillVO){
        Result result = billService.editWater(queryBillVO);
        return Result.success(result,"修改成功");
    }
    /**
     * 支出管理 修改数据
     * @param queryBillVO
     * @return
     */
    @PostMapping("/editWaterExpenditure")
    public Result<?> editWaterExpenditure(@RequestBody queryBillVO queryBillVO){
        Result result = billService.editWaterExpenditure(queryBillVO);
        return Result.success(result,"修改成功!");
    }

    @GetMapping("/findAllByChannel")
    public Result<?> findAllByChannel(){
        return Result.success();
    }


    /**
     * 全部收入数据导出
     * @param response
     * @throws IOException
     */
    @GetMapping("/incomeExport")
    public void IncomeExport(HttpServletResponse response) throws IOException {
        //查出全部收入数据
        List<IncomePO> list = billService.findIncomeAll();
        ServletOutputStream out = response.getOutputStream();
        ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX, true);
        String fileName = "收入详情";
        Sheet sheet = new Sheet(1, 0, IncomePO.class);
        //设置自适应宽度
        sheet.setAutoWidth(Boolean.TRUE);
        // 第一个 sheet 名称
        sheet.setSheetName("全部收入详情");
        writer.write(list, sheet);
        //通知浏览器以附件的形式下载处理，设置返回头要注意文件名有中文
        response.setHeader("Content-disposition", "attachment;filename=" + new String( fileName.getBytes("gb2312"), "ISO8859-1" ) + ".xlsx");
        writer.finish();
        response.setContentType("multipart/form-data");
        response.setCharacterEncoding("utf-8");
        out.flush();
    }

    @GetMapping("/ExpenseExport")
    public void expenseExport(HttpServletResponse response) throws IOException {
        //查出全部收入数据
        List<ExpensePO> list = billService.findExpenseExport();
        ServletOutputStream out = response.getOutputStream();
        ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX, true);
        String fileName = "支出详情";
        Sheet sheet = new Sheet(1, 0, ExpensePO.class);
        //设置自适应宽度
        sheet.setAutoWidth(Boolean.TRUE);
        // 第一个 sheet 名称
        sheet.setSheetName("全部支出数据");
        writer.write(list, sheet);
        //通知浏览器以附件的形式下载处理，设置返回头要注意文件名有中文
        response.setHeader("Content-disposition", "attachment;filename=" + new String( fileName.getBytes("gb2312"), "ISO8859-1" ) + ".xlsx");
        writer.finish();
        response.setContentType("multipart/form-data");
        response.setCharacterEncoding("utf-8");
        out.flush();
    }

    /**
     * 查询总订单数
     * @return
     */
    @GetMapping("/findAllOrders")
    public Result<?> findAllOrders(){
        int count = billService.findAllOrders();
        return Result.success(count,"总订单数："+count);
    }

    /**
     * 当日净流水
     * @return
     */
    @GetMapping("/findCleanWater")
    public Result<?> findCleanWater(){
        int count = billService.findCleanWater();
        return Result.success(count,"当日净流水:"+count);
    }
}
